package com.module.app.pop.anim;

import android.animation.ArgbEvaluator;
import android.animation.ValueAnimator;
import android.graphics.Color;

import androidx.interpolator.view.animation.FastOutSlowInInterpolator;

import android.view.View;

import com.lxj.xpopup.animator.PopupAnimator;

/**
 * Description: 背景Shadow动画器，负责执行半透明的渐入渐出动画
 * Create by dance, at 2018/12/9
 * 跟ShadowBgAnimator 一样，只是背景可控制颜色
 */
public class PopShadowBgAnimator extends PopupAnimator {

    public ArgbEvaluator argbEvaluator = new ArgbEvaluator();
    public int startColor = Color.TRANSPARENT;
    private int shadowBgColor = Color.parseColor("#7F000000");
    public boolean isZeroDuration = false;
    private int duration = 500;


    public PopShadowBgAnimator(View target, int shadowBgColor) {
        super(target, 500);
        this.shadowBgColor = shadowBgColor;
    }

    @Override
    public void initAnimator() {
        targetView.setBackgroundColor(startColor);
    }

    @Override
    public void animateShow() {
        ValueAnimator animator = ValueAnimator.ofObject(argbEvaluator, startColor, shadowBgColor);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                if (targetView != null) {
                    targetView.setBackgroundColor((Integer) animation.getAnimatedValue());
                }
            }
        });
        animator.setInterpolator(new FastOutSlowInInterpolator());
        animator.setDuration(isZeroDuration ? 0 : duration).start();
    }

    @Override
    public void animateDismiss() {
        ValueAnimator animator = ValueAnimator.ofObject(argbEvaluator, shadowBgColor, startColor);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                if (targetView != null) {
                    targetView.setBackgroundColor((Integer) animation.getAnimatedValue());
                }
            }
        });
        animator.setInterpolator(new FastOutSlowInInterpolator());
        animator.setDuration(isZeroDuration ? 0 : duration).start();
    }

    public int calculateBgColor(float fraction) {
        return (int) argbEvaluator.evaluate(fraction, startColor, shadowBgColor);
    }

    @Override
    public int getDuration() {
        return duration;
    }

    public void setDuration(int duration) {
        this.duration = duration;
    }
}
